<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2018\10\28 0028
 * Time: 10:03
 */
namespace app\admin\controller;
use think\Db;
use app\admin\model\Auths;
use think\Request;

class Privilege
{
//    public function __construct(Request $request)
//    {
//        Auths::login_auth($request);
//    }
    /**加载权限列表和分页查询
     * @return \think\response\View
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function show_list(Request $request){
        $tiaojian = $request->param('tiaojian',null);
        $limit = $request->param('limit',10);
        $page  = $request->param('page',1);
        $resul = Db::connect();
        if($tiaojian){
            $data = $resul->name('privilege')->where('name','like',"%$tiaojian%")->limit(($page-1)*$limit,$limit)->select();
            $datarow = $resul->name('privilege')->where('name','like',"%$tiaojian%")->count();
        }else{
            $data = $resul->name('privilege')->limit(($page-1)*$limit,$limit)->select();
            $datarow = $resul->name('privilege')->count();
        }
        return view('list',['data'=>$data,'datarow'=>$datarow,'tiaojian'=>$tiaojian]);
    }

    /**加载添加页面
     * @return \think\response\View
     */
public function show_add(){
        return view('add');
}
    /**添加
     * @return int|string
     */
    public function add(Request $request){
        $data = $request->post();
        if(!$data['name']){
            return '请输入权限名称';
        }
        if(Db::name('privilege')->insert($data)){
            return 1;
        }else{
            return '添加失败';
        }
    }

    /**加载编辑
     * @return string|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit(Request $request){
        $id = $request->param('id');
        if(!$id){
            return '请输入要修改的id';
        }
        $data = Db::name('privilege')->where('id',$id)->find();
        return view('edit',['data'=>$data]);
    }

    /**更新
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function update(Request $request){
        $data  = $request->post();
        if(!$data['id']||!$data['name']){
            return '请输入必要的数据';
        }
        if(Db::name('privilege')->where('id',$data['id'])->update($data)){
            return 1;
        }else{
            return '什么都没改变';
        }
    }

    /**删除数据
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del(Request $request){
        $id = $request->param('id',null);
        if(!$id){
            return '请输入要删除的id';
        }
            if(Db::name('privilege')->where('id','in',$id)->delete()){
                return 1;
            }else{
                return '删除失败';
            }
    }

    /**分页
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function page(Request $request){
        $tiaojian = $request->param('tiaojian',null);
        $limit = $request->param('limit',10);
        $page  = $request->param('page',1);
        if($tiaojian){
            $data = Db::name('privilege')->where('name','like',"%$tiaojian%")->limit(($page-1)*$limit,$limit)->select();
        }else {
            $data = Db::name('privilege')->limit(($page - 1) * $limit, $limit)->select();
        }
        return $data;
    }
}